package com.hctforgreen.greenservice.model;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import com.hctforgreen.greenservice.utils.HctConstants;

/**
 * 6.11、获取随机产生试题
 * @author Tallen Zhai
 *
 */
public class RandomExamListEntity extends BaseEntity {

	private static final long serialVersionUID = -6178589713947032141L;

	public String state = "";
	public String ids = "";
	public List<String> idsList = null;
	
	public final static String STATE = "state";
	private final static String BODY = "body";
	private final static String IDS = "ids";
	public static RandomExamListEntity parse(JSONObject jsonObject)throws JSONException{
		RandomExamListEntity bean = new RandomExamListEntity();
		if(jsonObject.has(STATE)){
			bean.state = jsonObject.getString(STATE);
		}
		/**
		 * 解析body节点
		 */
		if (jsonObject.has(BODY)) {
			
			JSONObject bodyObject = jsonObject.getJSONObject(BODY);
				
			if(bodyObject.has(IDS)){
				String ids = bodyObject.getString(IDS);
				
				addIdsList(bean, ids);
				bean.ids = ids;
			}
			
		}
		/**
		 * -1表示:无数据
		 */
		if(bean.state.equals(HctConstants.RESULT_STATE_FAILD)
				&& (bean.ids == null || bean.ids.equals(""))){
			bean.idsList = new ArrayList<String>();
		}
		return bean;
	}
	private static void addIdsList(RandomExamListEntity bean, String ids) {
		String[] arrayIds = ids.split(";");
		if (arrayIds != null && arrayIds.length > 0){
			bean.idsList = new ArrayList<String>();
			for(String id : arrayIds){
				bean.idsList.add(id);
			}
		}
		
	}
}
